package com.zhangtai.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


/**
 * 绑定获取工号队列
 */
@Configuration
public class GetUserCodeMqConfig {
    /**
     * 业务交换机
     */
    public static final String SMS_EXCHANGE = "GET_USER_CODE_DIRECT";
    /**
     * 业务队列名称
     */
    public static final String SMS_QUEUE = "GET_USER_CODE_QUEUE";
    /**
     * 死信队列交换机名称
     */
    public static final String   DEAD_LETTER_EXCHANGE="GET_USER_CODE_DEAD_LETTER_DIRECT";
/**
 * 死信队列名称
 */
public static final String   DEAD_LETTER_QUEUE = "GET_USER_CODE_DEAD_LETTER_QUEUE";
    /**
     * RouteKey
     */
    public static final String SMS_ROUTING_KEY = "GET_USER_CODE";
    /**
     * 配置死信交换机
     * @return
     */
    @Bean
    public DirectExchange  deadLetterDirectExchange(){
        return new DirectExchange(DEAD_LETTER_EXCHANGE);
    }
/**
 * 配置死信队列
 * @return
 */
@Bean
public Queue  deadLetterQueue(){
    return new Queue(DEAD_LETTER_QUEUE);
}
    /**
     * 绑定死信队列和死信交换机
     * @return
     */
    @Bean
    Binding deadLetterBindingDirect() {
        return BindingBuilder.bind(deadLetterQueue()).to(deadLetterDirectExchange()).with(SMS_ROUTING_KEY);
    }
/**
 * 配置队列
 * @return
 */
@Bean
public Queue smsDirectQueue() {
    Map<String, Object> args = new HashMap<>(16);
    // 队列消息过期时间
    args.put("x-message-ttl", 25000);
    args.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE);
    args.put("x-dead-letter-routing-key", SMS_ROUTING_KEY);
    //  args.put("x-expires", 5000);队列过期时间
    // args.put("x-max-length",5 );
    return new Queue(SMS_QUEUE, true,false,false,args);
}
    /**
     * 配置交换机
     * @return
     */
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(SMS_EXCHANGE);
    }


    /**
     * 交换机与队列绑定
     * @return
     */
    @Bean
    Binding smsBindingDirect() {
        return BindingBuilder.bind(smsDirectQueue()).to(directExchange()).with(SMS_ROUTING_KEY);
    }


}
